package com.example.demo.controller;


import com.example.demo.entity.Act_template;
import com.example.demo.pojo.PlannerResult;
import com.example.demo.repository.Act_templateRepository;
import com.example.demo.vo.Act_templateVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/template")
public class ActTemplateController {

    @Autowired
    Act_templateRepository act_templateRepository;

    //创建活动模板
    @PostMapping("/add")
    PlannerResult<String> create_act_template(@RequestBody Act_templateVo actTemplateVo)
    {
        Act_template actTemplate=new Act_template();
        actTemplate.setInfo(actTemplateVo.getInfo());
        actTemplate.setTem_name(actTemplateVo.getTem_name());
        actTemplate.setName(actTemplateVo.getName());
        actTemplate.setLocation(actTemplateVo.getLocation());
        Act_template result = act_templateRepository.save(actTemplate);
        if(result.getId()!=null)
        {
            return PlannerResult.success("活动模板创建成功");
        }
        else return PlannerResult.error("活动模板创建失败");
    }
    @PostMapping("/list")
    PlannerResult<Page<Act_template>> list_act_template (@RequestBody Map<String, Integer> params)
    {    Integer page = params.get("page");
        Integer size = params.get("size");
        Pageable pageable = PageRequest.of(page - 1, size);
        Page<Act_template> result = act_templateRepository.findAll(pageable);
        return PlannerResult.success(result);

    }
    @PostMapping("/update")
    PlannerResult <String> update_act_template(@RequestBody Act_templateVo actTemplateVo)
    {
        Act_template actTemplate=act_templateRepository.findById(actTemplateVo.getId()).get();
        actTemplate.setId(actTemplateVo.getId());
        actTemplate.setInfo(actTemplateVo.getInfo());
        actTemplate.setTem_name(actTemplateVo.getTem_name());
        actTemplate.setName(actTemplateVo.getName());
        actTemplate.setLocation(actTemplateVo.getLocation());
        Act_template result = act_templateRepository.save(actTemplate);
        if(result.getId()!=null)
        {
            return PlannerResult.success("活动模板修改成功");
        }
        else return PlannerResult.error("活动模板修改失败");
    }

    @PostMapping("/delete/{template_id}")
    PlannerResult<String> delete_act_template(@PathVariable("template_id") Integer template_id)
    {
        if(act_templateRepository.existsById(template_id)){
            act_templateRepository.deleteById(template_id);
            return  PlannerResult.success("已经删除");
        }
        else{
            return PlannerResult.error("删除失败");
        }
    }
}
